package com.doculibre.constellio.connector.persistence.services;

import java.util.List;

import javax.persistence.Query;

import com.doculibre.constellio.connector.persistence.config.PersistenceContext;
import com.doculibre.constellio.connector.persistence.model.TweetDoc;

/**
 * Twitter implementation for CRUD services.
 * 
 * @author France Labs
 * 
 * @param <T>
 */
public class TweetServices extends BaseCRUDServicesImpl<TweetDoc>{

	public TweetServices(Class<TweetDoc> entityClass,
			PersistenceContext persistenceContext) {
		super(entityClass, persistenceContext);
	}

	/**
	 * 
	 * Query the database in order to get the list of the ID of the Tweets that have been persisted in DB
	 * 
	 * @param fromID (fromID must be > 0)
	 * @param toID (if toID <= 0, the parameter will be omitted)
	 * @return list of IDs
	 */
	@SuppressWarnings("unchecked")
	public List<Long> getListIDTweets(Long fromID, Long toID) {
		
		Query query = null;
		
		if (toID <= 0L){
			query = getEntityManager().createQuery("select id from " + entityClass.getName() + " o where id>=:fromid order by id desc");
		} else {
			query = getEntityManager().createQuery("select id from " + entityClass.getName() + " o where id>=:fromid and id<=:toid order by id desc");
			query.setParameter("toid", toID);
		}

		query.setParameter("fromid", fromID);
		
		return (List<Long>) query.getResultList();
	}
	

	/**
	 * 
	 * Query the database in order to get the list of the ID of the Tweets that have been persisted in DB
	 * 
	 * @param fromID (fromID must be > 0)
	 * @return list of IDs
	 */
	public List<Long> getListIDTweets(Long fromID) {
		return getListIDTweets(fromID, 0L);
	}
	
	
	

}
